package com.tuanbiao.ui.auth.adapter;

import android.text.TextUtils;

import androidx.annotation.ColorRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.abiao.lib_utils.TimeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hzjq.data.minbao.entity.AuthEntity;
import com.tuanbiao.R;
import com.tuanbiao.utils.LibCollections;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import timber.log.Timber;

/**
 * @author abiao
 * @date 2022/5/27 16:40
 * @description:
 */
public class AuthOfflineAdapter extends BaseQuickAdapter<AuthEntity.Lgs.Lg, BaseViewHolder> {
    private final String TAG = this.getClass().getSimpleName();

    public AuthOfflineAdapter() {
        super(R.layout.item_detonator_plan_auth_offline);
    }

    private ArrayList<AuthEntity.Zbqys.Zbqy> zbqys = new ArrayList<AuthEntity.Zbqys.Zbqy>();

    public void setAuthZbqy(ArrayList<AuthEntity.Zbqys.Zbqy> zbqy) {
        zbqys.clear();
        zbqys.addAll(zbqy);
    }

    private boolean validityPeriod(String time) {
        if (TextUtils.isEmpty(time)) {
            return findPeriodFromZbqy();
        }
        return TimeUtils.string2Millis(time) < TimeUtils.getNowMills();
    }


    private boolean findPeriodFromZbqy() {
        try {
            for (int i = 0; i < zbqys.size(); i++) {
                AuthEntity.Zbqys.Zbqy zbqy = zbqys.get(i);
                if (!TextUtils.isEmpty(zbqy.getZbjzsj())) {
                    return TimeUtils.string2Millis(zbqy.getZbjzsj()) < TimeUtils.getNowMills();
                }
            }
        } catch (Exception e) {

        }
        return false;
    }


    /**
     * 选择对象集合
     */
    private final Map<Integer, AuthEntity.Lgs.Lg> mSelectSet = new HashMap<>();

    @Override
    protected void convert(@NonNull BaseViewHolder holder, AuthEntity.Lgs.Lg entity) {
        boolean selected = mSelectSet.containsKey(holder.getLayoutPosition());
        int itemIndex = holder.getLayoutPosition() + 1;
        Timber.i("DetonatorRegisterAdapter#entity:" + entity);
        holder.setBackgroundResource(R.id.ll_content, selected ? R.drawable.bg_solid_warning_r_2 : R.drawable.bg_solid_white_stroke_primary_r2);
        boolean period = validityPeriod(entity.getYxq());
        holder.setText(R.id.tv_item, String.valueOf(itemIndex))
                .setText(R.id.tv_code, entity.getUid());
//                .setTextColorRes(R.id.tv_status, period ? R.color.textSuccess : R.color.textDanger)
//                .setText(R.id.tv_status, period ? "有效" : "失效");
    }

    @Override
    public void setNewInstance(@Nullable List<AuthEntity.Lgs.Lg> list) {
        mSelectSet.clear();
        super.setNewInstance(list);
    }

    public void remove(List<AuthEntity.Lgs.Lg> data) {
        for (AuthEntity.Lgs.Lg entity : data) {
            mSelectSet.remove(getItemPosition(entity));
            super.remove(entity);
        }

    }

    public void remove(AuthEntity.Lgs.Lg entity) {
        mSelectSet.remove(getItemPosition(entity));
        super.remove(entity);
    }

    public void notifyItemChanged(AuthEntity.Lgs.Lg entity) {
        notifyItemChanged(getItemPosition(entity));
    }

    @ColorRes
    private int getTextColor(boolean isSelected) {
        return isSelected ? R.color.textWhite : R.color.textPrimary;
    }

    public void toggleSelect(int position) {
        if (position == 0) {
            return;
        }
        Timber.i(TAG + "#toggleSelect:" + position + ",select:" + mSelectSet.containsKey(position));
        if (mSelectSet.containsKey(position)) {
            mSelectSet.remove(position);
            notifyItemChanged(position);
        } else {
            mSelectSet.clear();
            mSelectSet.put(position, getItem(position));
            notifyDataSetChanged();
        }
    }

    public List<AuthEntity.Lgs.Lg> getSelectedData() {
        if (mSelectSet.isEmpty()) {
            return new ArrayList<>(0);
        }
        List<AuthEntity.Lgs.Lg> list = new ArrayList<>(LibCollections.size(mSelectSet));
        list.addAll(mSelectSet.values());
        return list;
    }

    public void removeSelected() {
        for (AuthEntity.Lgs.Lg lg : getSelectedData()) {
            remove(lg);
            notifyItemChanged(lg);
        }
    }

    public void select(AuthEntity.Lgs.Lg capRecordEntity) {
        for (AuthEntity.Lgs.Lg entity : getData()) {
            if (TextUtils.equals(capRecordEntity.getUid(), entity.getUid())) {
                mSelectSet.clear();
                mSelectSet.put(getItemPosition(entity), entity);
                notifyDataSetChanged();
                break;
            }
        }
    }

    public boolean contains(AuthEntity.Lgs.Lg newCaps) {
        for (AuthEntity.Lgs.Lg entity : getData()) {
            if (TextUtils.equals(newCaps.getUid(), entity.getUid())) {
                return true;
            }
        }
        return false;
    }

    public int getPosition(String uid) {
        for (int i = 0; i < getItemCount(); i++) {
            if (TextUtils.equals(getData().get(i).getUid(), uid)) {
                return i;
            }
        }
        return 0;
    }
}
